﻿public class Solution {
    public int Rob(int[] nums)
    {
        if (nums is null || nums.Length == 0) return 0;
        if (nums.Length == 1) return nums[0];
        
        // 分两种情况考虑
        int res1 = RobRange(nums, 0, nums.Length - 2);
        int res2 = RobRange(nums, 1, nums.Length - 1);

        return Math.Max(res1, res2);
    }

    private int RobRange(int[] nums, int startIndex, int endIndex)
    {
        int[] dp = new int[endIndex - startIndex + 1];
        dp[0] = nums[startIndex];
        if (dp.Length == 1) return dp[0];
        dp[1] = Math.Max(nums[startIndex], nums[startIndex + 1]);

        for (int i = 2; i < dp.Length; i++)
        {
            dp[i] = Math.Max(dp[i - 2] + nums[startIndex + i], dp[i - 1]);
        }

        return dp[^1];
    }

    public static void Main(string[] args)
    {
        int[] nums = [2, 3, 2];
        Solution solution = new Solution();
        int res = solution.Rob(nums);
        Console.WriteLine(res);
    }
}